package com.haoxuewa.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoxuewa.dao.AdminDao;
import com.haoxuewa.dao.UserDao;
import com.haoxuewa.dao.VisitQuantityDao;
import com.haoxuewa.entity.Message;
import com.haoxuewa.entity.User;
import com.haoxuewa.entity.VisitQuantity;
import com.haoxuewa.service.UserService;
import com.haoxuewa.service.VisitQuantityService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class VisitQuantityServiceImpl extends ServiceImpl<VisitQuantityDao, VisitQuantity> implements VisitQuantityService {


    private final VisitQuantityDao visitQuantityDao;

    @Autowired
    public VisitQuantityServiceImpl(VisitQuantityDao visitQuantityDao) {
        this.visitQuantityDao = visitQuantityDao;
    }

    @Override
    public int[] weekVisited(int n) {
        int m = 7 - n;
        List<Map<String, Object>> list_1 = visitQuantityDao.selectMaps(new QueryWrapper<VisitQuantity>().select("dayOfWeek", "quantity").last("limit " + n));
        List<Map<String, Object>> list_2 = visitQuantityDao.selectMaps(new QueryWrapper<VisitQuantity>().select("dayOfWeek", "quantity").last("limit " + n + "," + m));
        int[] front = new int[n];
        int[] back = new int[m];
        int i = 0;
        for (Map<String, Object> map : list_1) {
            front[i] = Integer.parseInt(String.valueOf(map.get("quantity")));
            i++;
        }
        i = 0;
        for (Map<String, Object> map : list_2) {
            back[i] = Integer.parseInt(String.valueOf(map.get("quantity")));
            i++;
        }
        ArrayUtils.reverse(front);
        ArrayUtils.reverse(back);
        return ArrayUtils.addAll(front, back);
    }

}
